"""
1、函数应用：学生管理系统
2、将内容保存到文件中
"""
def print_menu():
    print("      学生管理系统 V1.0")
    print(" 1:添加学生")
    print(" 2:删除学生")
    print(" 3:修改学生")
    print(" 4:查询学生")
    print(" 5:显示所有学生")
    print(" 6:保存数据")
    print(" 7:退出系统")
    print("---------------------------")

# list_info = [{'name': 'aaa', 'tel': '444', 'qq': '55'},{'name': 'bbb', 'tel': '444', 'qq': '55'}]
list_info = []


def add_student():  # 1 添加学生信息
    global list_info
    new_name = input("请输入学生姓名:")
    new_tel = input("请输入手机号:")
    new_qq = input("请输入QQ:")
    for line in list_info:
        if line['name'] == new_name:
            print("学生'%s'已经被占用,请重新输入" % new_name)
            return
    new_info = {}
    new_info['name'] = new_name
    new_info['tel'] = new_tel
    new_info['qq'] = new_qq
    print('添加学生信息%s' % new_info)
    list_info.append(new_info)


def del_student():      # 2 删除学生信息
    global list_info
    del_name = input("请输入要删除的学生姓名:")
    for line in list_info:
        if line['name'] == del_name:
            result = input('你确定要删除么?yes or no')
            if result == 'yes':
                list_info.remove(line)
                print("学生'%s'删除成功！" % line['name'])
                break
            elif result == 'no':
                print("学生'%s'取消删除！" % line['name'])
                break
            else:
                print("输入指令有误，请重新输入！")
                break
    else:
        print("学生'%s'不存在！" % del_name)


def update_student():    # 3 修改学生信息
    global list_info
    update_name = input("请输入要修改的学生姓名:")
    for line in list_info:
        if len(list_info) > 0:
            for list_index in range(len(list_info)):
                if list_info[list_index]['name'] == update_name:
                    line['name'] = input("请输入新学生姓名:")
                    line['tel'] = input("请输入新手机号:")
                    line['qq'] = input("请输入新QQ:")
                    print('修改后学生信息%s' % line)
                    break
            else:
                print("学生'%s'不存在！" % update_name)
                break
        break


def show_student():     # 4 搜索学生信息
    select_name = input("请输入要查询的学生姓名:")
    for line in list_info:
        if line['name'] == select_name:
            print(line)
    else:
        print("没有您要找的信息")


def print_all_info():   # 5 打印所有学生信息
    # print(list_info)
    """遍历学生信息"""
    print("序号\t\t姓名\t\t手机号\t\tQQ")
    i = 0
    for temp in list_info:   # temp是一个字典
        print("%d\t%s\t\t%s\t\t%s" % (i, temp['name'], temp['tel'], temp['qq']))
        i += 1


def save_data():   # 6 储存数据到文件
    file = open('student_data.data', 'a')
    file.write(str(list_info))
    file.close()


def load_data():
    global list_info
    file = open('student_data.data')
    content = file.read()
    list_info = eval(content)    # 将字符串str对象转化为有效的表达式参与求值运算并返回值
    file.close()


def main():    # 用来控制整个流程
    load_data()
    while True:
        print_menu()   # 打印功能
        get_num = int(input("请输入操作序号："))
        if get_num == 1:
            add_student()
        elif get_num == 2:
            del_student()
        elif get_num == 3:
            update_student()
        elif get_num == 4:
            show_student()
        elif get_num == 5:
            print_all_info()
        elif get_num == 6:
            save_data()
        elif get_num == 7:
            print("成功退出学生管理系统")
            break
        else:
            print("输入有误，请重新输入！")

# 开始程序
main()

